<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 后台菜单管理
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class Archivescon extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		$this->load->model('JayphaTemplate');
		$this->adminCurrentTemplate = $this->JayphaDB->getConfigValue('adminCurrentTemplate');
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}
	
	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());		
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = "archives";
		$channel_tv = "channel";
		$channel_id = $this->uri->segment(4);
		$subjoin_condition = array('id'=>$channel_id);
		$subjoin = $this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$channel_tv,$subjoin_condition,'',true);
		if(count($subjoin)>0){
			$channel_title = $subjoin[0]->title;
			$channel_subjoinTable = $subjoin[0]->subjoinTable;
			$channel_main_con = $subjoin[0]->adminMainCon;
			$channel_edit_con = $subjoin[0]->adminEditCon;
			$channel_identifier = $subjoin[0]->identifier;
		}else{
			exit;
		}
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$active = $this->input->post('active');
			$del = $this->input->post('del');
			$id = $this->input->post('id');
			if(is_array($id)){
				$id = implode(',',$id);
			}
			$data['disabled'] = $this->input->post('disabled');
			$data['ismake'] = $this->input->post('ismake');
			$data['categoryId'] = $this->input->post('categoryId');
			$data['title'] = $this->input->post('title');
			$data['keywords'] = $this->input->post('keywords');
			$data['upTimeStart'] = $this->input->post('upTimeStart');
			$data['upTimeEnd'] = $this->input->post('upTimeEnd');
			$data['createTimeStart'] = $this->input->post('createTimeStart');
			$data['createTimeEnd'] = $this->input->post('createTimeEnd');
		}else{
			$data['disabled'] = $this->input->get('disabled');
			$data['ismake'] = $this->input->get('ismake');
			$data['categoryId'] = $this->input->get('categoryId');
			$data['title'] = $this->input->get('title');
			$data['keywords'] = $this->input->get('keywords');
			$data['upTimeStart'] = $this->input->get('upTimeStart');
			$data['upTimeEnd'] = $this->input->get('upTimeEnd');
			$data['createTimeStart'] = $this->input->get('createTimeStart');
			$data['createTimeEnd'] = $this->input->get('createTimeEnd');
		}
		$data['title'] = urldecode($data['title']);
		$data['keywords'] = urldecode($data['keywords']);
		$condition = array('channelId'=>$channel_id);
		$db_like_key = array();
		$db_like_value = array();
		if(strlen($data['disabled'])>0){
			$condition['disabled'] = $data['disabled'];
		}		
		if(strlen($data['ismake'])>0){
			$condition['ismake'] = $data['ismake'];
		}
		if(strlen($data['categoryId'])>0){
			$condition['categoryId'] = $data['categoryId'];
		}
		if(strlen($data['title'])>0){
			array_push($db_like_key,'upper(title)');
			array_push($db_like_value,strtoupper($data['title']));
		}
		if(strlen($data['upTimeStart'])>0){
			$condition['updateTime >= '] = strtotime($data['upTimeStart']);
		}
		if(strlen($data['upTimeEnd'])>0){
			$condition['updateTime <= '] = strtotime($data['upTimeEnd']) + 24*60*60;
		}
		if(strlen($data['createTimeStart'])>0){
			$condition['createtime >= '] = strtotime($data['createTimeStart']);
		}
		if(strlen($data['createTimeEnd'])>0){
			$condition['createtime <= '] = strtotime($data['createTimeEnd']) + 24*60*60;
		}
		if(count($db_like_key)>0){
			$condition['db_like'] = array_combine($db_like_key,$db_like_value);
		}
		unset($db_like_key);
		unset($db_like_value);
		$where = $condition;
		if($this->input->server('REQUEST_METHOD')=='POST'){
			switch (strtoupper($active)){
				case 'DEL':
					$del_condition = $del_subjointable_condition = array();
					if(strlen($del)==0){
						$condition['db_where_in'] = array('id',$id);
					}
					foreach($condition as $k=>$v){
						if(strtolower($k)==='channelid' || strtolower($k)==='categoryid' || strtolower($k)==='db_where_in'){
							$del_condition[$k] = $v;
						}
					}
					$del_subjointable_condition = $del_condition;
					if(array_key_exists('db_where_in',$del_subjointable_condition)){
						$del_subjointable_condition['db_where_in'][0]='aid';
					}	
					$operationData = array(
						'adminId'=>$this->session->userdata('adminUserId'),
						'title'=>'删除文档',
						'url'=>$this->input->server('REQUEST_URI'),
						'remark'=>'删除文档，删除内容：'.json_encode($this->JayphaDB->getSingleDB($this->dbTablePrefix.$tvName,$del_condition))
					);
					$this->db->trans_start();
					$this->JayphaDB->setAdminOperation($operationData);				
					$this->JayphaDB->delDB($this->dbTablePrefix.$channel_subjoinTable,$del_subjointable_condition);
					$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,$del_condition);
					$this->db->trans_complete();
					unset($del_condition);
					unset($del_subjointable_condition);
					break;
				case 'SETPARAM':
					//批量设置属性
					$data['click'] = $this->input->post('click');
					$data['flag'] = $this->input->post('flag');
					$updata = array();
					if(strlen($data['disabled'])>0){
						$updata['disabled'] = $data['disabled'];
					}
					if(strlen($data['ismake'])>0){
						$updata['ismake'] = $data['ismake'];
					}
					if(is_array($data['flag'])){
						$flag = '';
						foreach($data['flag'] as $flag_item){
							if($flag=='')
								$flag = $flag_item;
							else
								$flag .= ','.$flag_item;
						}
						$updata['flag'] = $flag;
					}else{
						$updata['flag'] = null;
					}
					if(strlen($data['click'])>0){
						if(is_numeric($data['click'])){
							$updata['click'] = (int)$data['click'];
						}
					}
					$condition2['db_where_in'] = array('id',$id);
					$this->JayphaDB->updateDB($updata,$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'DISABLED':
					//启用状态
					$condition2['db_where_in'] = array('id',$id);
					$disabled = $this->input->post('disabled');
					$this->JayphaDB->updateDB(array('disabled'=>$disabled),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'REMOVE':
					//移动
					$categoryId = $this->input->post('categoryId');
					$condition2['db_where_in'] = array('id',$id);
					$condition3['db_where_in'] = array('aid',$id);
					$moveData = array('categoryId'=>$categoryId);
					$this->db->trans_start();					
					$channelDB = $this->dbTablePrefix.$tvName;
					$channelList = $this->JayphaDB->getDB(0,0,'channelId',$channelDB,$condition2,'',true);
					foreach($channelList as $item){
						$moveData['channelId'] = $item->channelId;
						$subjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.'channel',array('id'=>$item->channelId),'');
						$this->JayphaDB->updateDB($moveData,$this->dbTablePrefix.$subjoinTable,$condition3);
					}					
					$this->JayphaDB->updateDB($moveData,$this->dbTablePrefix.$tvName,$condition2);
					$this->db->trans_complete();
					break;
			}
		}
		$config['base_url'] = site_url($channel_main_con.'/');
		$config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
		$config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
		$config['uri_segment'] = 5;
		$config['num_links'] = 6;
		$config['first_link'] = '|<';
		$config['last_link'] = '>|';
		$config['next_link'] = '>>';
		$config['prev_link'] = '<<';
		$this->pagination->initialize($config);
		$data['currentSite'] = '内容管理<span class="split">>></span>'.$channel_title.'管理<span class="split">>></span>文档列表';
		$data['phpSelf'] = $config['base_url'];
		$data['pageTag'] = $channel_title.'管理';
		$data['editPage'] = site_url('admin/archivescon/edit');//site_url($channel_edit_con);
		$data['editTitle'] = '修改文档';
		$data['addTitle'] = '添加文档';
		$data['perm'] = $channel_identifier;
		$data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
		$data['tvName'] = $tvName;
		$order = 'id';
		$num = $this->uri->segment(5,0);
		$data['archivesList'] = $this->JayphaDB->getDB($config['per_page'],$num,'',$this->dbTablePrefix.$tvName,$where,$order,false);
		foreach($data['archivesList'] as $archives_item){
			if($archives_item->categoryId==0)
				$archives_item->categoryTitle = '未分配';
			else{
				$archives_item->categoryTitle = $this->JayphaDB->getDBValue('title',$this->dbTablePrefix.'category',array('id'=>$archives_item->categoryId),'');
			}
			$archives_item->adminEditCon = $channel_edit_con;
		}
		$categoryAll = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.'category',array('channelId'=>$channel_id),'parentId,id',false));
		$data['categoryList'] = $this->JayphaDB->getChildrenList($categoryAll,0,1);
		$this->load->view("admin/$this->adminCurrentTemplate/archivescon",$data);
	}
	
	function edit(){
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'archives';
		$data['tvName'] = $tvName;
		$data['perm'] = 'archives';
		$data['channelList'] = $this->JayphaDB->getDB(0,0,'id,title',$this->dbTablePrefix.'channel',array('disabled'=>0),'',false);
		$this->load->view("admin/$this->adminCurrentTemplate/archivesEdit",$data);
	}
}

/* End of file archivescon.php */
/* Location: ./application/controllers/admin/archivescon.php */